package cn.xpf.study.controller.backend;

import cn.xpf.study.dao.LessonItemMapper;
import cn.xpf.study.dao.LessonMapper;
import cn.xpf.study.dao.LessonVideoMapper;
import cn.xpf.study.dao.UserMapper;
import cn.xpf.study.model.Lesson;
import cn.xpf.study.model.LessonItem;
import cn.xpf.study.model.LessonVideo;
import cn.xpf.study.model.User;
import cn.xpf.study.model.vo.LessonVo;
import cn.xpf.study.service.LessonService;
import cn.xpf.study.utils.PreviewUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Create on 2021/5/15
 *
 * @author shuxinsheng@qiyi.com
 **/
@RequestMapping("/backend")
@Controller
public class BackendController {

    @Autowired
    LessonMapper lessonMapper;

    @Autowired
    LessonItemMapper lessonItemMapper;

    @Autowired
    LessonVideoMapper lessonVideoMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    LessonService lessonService;

    @RequestMapping("/login")
    public String login() {
        return "backend/login";
    }

    @RequestMapping("/user/login")
    @ResponseBody
    public Object userLogin(
            HttpSession httpSession,
            @RequestParam("username") String username,
            @RequestParam("password") String password) {
        Map<String, Object> resMap = new HashMap<>();
        User user = userMapper.findOneByUsername(username);
        if (user == null) {
            resMap.put("msg", "用户不存在");
        } else {
            if (user.deleted == 1) {
                resMap.put("msg", "非法用户");
            } else if (!password.equals(user.password)) {
                resMap.put("msg", "密码错误");
            } else {
                httpSession.setAttribute("admin_logged", "1");
                httpSession.setAttribute("admin_username", username);
                resMap.put("code", "1");
            }
        }
        return resMap;
    }

    @RequestMapping("/logout")
    public String logout(HttpSession httpSession) {
        httpSession.removeAttribute("logged");
        return "backend/login";
    }

    @RequestMapping("/index")
    public String index(HttpSession httpSession) {
        if (isNotAdminLogged(httpSession)) {
            return "redirect:/backend/login";
        }
        return "backend/index";
    }

    @RequestMapping("/lesson_list")
    public String lessonList(Model model, HttpSession httpSession) {
        if (isNotAdminLogged(httpSession)) {
            return "redirect:/backend/login";
        }
        List<Lesson> lessonList = lessonMapper.findAll();
        List<LessonVo> lessonVos = new ArrayList<>();
        for (Lesson lesson : lessonList) {
            LessonItem lessonItem = lessonItemMapper.findOne(lesson.id);
            LessonVo lessonVo = new LessonVo(lesson, lessonItem);
            lessonVos.add(lessonVo);
        }
        model.addAttribute("lessonVos", lessonVos);
        return "backend/lesson_list";
    }

    @RequestMapping("/lesson_create")
    public String lessonCreate(Model model, HttpSession httpSession) {
        if (isNotAdminLogged(httpSession)) {
            return "redirect:/backend/login";
        }
        Lesson lesson = new Lesson();
        LessonVo lessonVo = new LessonVo(lesson, new LessonItem());
        model.addAttribute("lessonVo", lessonVo);
        return "backend/lesson_create";
    }

    @RequestMapping("/lesson_edit")
    public String lessonEdit(HttpSession httpSession, Model model,
            @RequestParam("id") int lessonId) {
        if (isNotAdminLogged(httpSession)) {
            return "redirect:/backend/login";
        }
        Lesson lesson = lessonMapper.findById(lessonId);
        LessonItem lessonItem = lessonItemMapper.findOne(lessonId);
        LessonVo lessonVo = new LessonVo(lesson, lessonItem);
        model.addAttribute("lessonVo", lessonVo);
        return "backend/lesson_edit";
    }

    @RequestMapping(value = "/lesson_preview", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object lessonPreview(HttpSession httpSession, @RequestBody LessonVo lessonVo) {
        if (isNotAdminLogged(httpSession)) {
            return "redirect:/backend/login";
        }
        Long hashCode = PreviewUtil.addLessonVo(lessonVo);
        System.out.println(hashCode);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("hash", hashCode);
        return resMap;
    }

    @RequestMapping(value = "/lesson_preview_show")
    public Object lessonPreviewShow(HttpSession httpSession, Model model,
            @RequestParam("hash") long hash) {
        if (isNotAdminLogged(httpSession)) {
            return "redirect:/backend/login";
        }
        LessonVo lessonVo = PreviewUtil.getLessonVo(hash);
        model.addAttribute("lesson", lessonVo);
        model.addAttribute("contentList", lessonVo.getContent());
        return "lesson_preview";
    }

    @RequestMapping(value = "/lesson_save", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object lessonCreate(@RequestBody LessonVo lessonVo) {
        lessonService.createLesson(lessonVo);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("code", "0");
        return resMap;
    }

    @RequestMapping(value = "/lesson_update", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object lessonUpdate(@RequestBody LessonVo lessonVo) {
        lessonService.updateLesson(lessonVo);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("code", "0");
        return resMap;
    }

    @RequestMapping(value = "/lesson_delete")
    @ResponseBody
    public Object lessonDelete(@RequestParam("id") int id) {
        lessonMapper.deleteOne(id);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("code", "0");
        return resMap;
    }

    @RequestMapping(value = "/lesson_video_list")
    public Object lessonPreviewShow(HttpSession httpSession, Model model,
            @RequestParam("lesson_id") int lessonId) {
        if (isNotAdminLogged(httpSession)) {
            return "redirect:/backend/login";
        }
        List<LessonVideo> lessonVideos = lessonVideoMapper.findAllByLessonId(lessonId);
        model.addAttribute("lessonId", lessonId);
        model.addAttribute("lessonVideos", lessonVideos);
        return "backend/lesson_video_list";
    }

    @RequestMapping(value = "/lesson_video_create")
    public Object lessonVideoCreate(HttpSession httpSession, Model model,
            @RequestParam("lesson_id") int lessonId) {
        if (isNotAdminLogged(httpSession)) {
            return "redirect:/backend/login";
        }
        LessonVideo lessonVideo = new LessonVideo();
        model.addAttribute("lessonId", lessonId);
        model.addAttribute("lessonVideo", lessonVideo);
        return "backend/lesson_video_create";
    }

    @RequestMapping(value = "/lesson_video_edit")
    public Object lessonVideoUpdate(HttpSession httpSession, Model model,
            @RequestParam("id") int videoId) {
        if (isNotAdminLogged(httpSession)) {
            return "redirect:/backend/login";
        }
        LessonVideo lessonVideo = lessonVideoMapper.findOneById(videoId);
        model.addAttribute("lessonVideo", lessonVideo);
        model.addAttribute("lessonId", lessonVideo.lessonId);
        return "backend/lesson_video_edit";
    }

    @RequestMapping(value = "/lesson_video_save", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object lessonVideoCreate(@RequestBody LessonVideo lessonVideo) {
        lessonVideoMapper.insertOne(lessonVideo);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("code", "0");
        return resMap;
    }

    @RequestMapping(value = "/lesson_video_update", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object lessonVideoUpdate(@RequestBody LessonVideo lessonVideo) {
        Map<String, Object> resMap = new HashMap<>();
        if (lessonVideo.id <= 0) {
            resMap.put("code", 1);
        } else {
            lessonVideoMapper.updateOne(lessonVideo);
            resMap.put("code", "0");
        }
        return resMap;
    }

    @RequestMapping(value = "/lesson_doc")
    public Object lessonDoc() {
        return "backend/lesson_doc";
    }

    @RequestMapping(value = "/lesson_video_delete")
    @ResponseBody
    public Object lessonVideoDelete(@RequestParam("id") int id) {
        lessonVideoMapper.deleteOne(id);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("code", "0");
        return resMap;
    }

    private boolean isNotAdminLogged(HttpSession httpSession) {
        if (httpSession == null || !"1".equals(httpSession.getAttribute("admin_logged"))) {
            return true;
        }
        return false;
    }
}
